package org.asiainfo.system.service.impl;

import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.system.domain.vo.SysLoginInfoVo;
import org.asiainfo.system.domain.bo.SysLoginInfoBo;
import org.asiainfo.system.domain.SysLoginInfo;
import org.asiainfo.system.service.SysLoginInfoService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.system.mapper.SysLoginInfoMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.Date;
import java.util.Map;
import java.util.Collection;
import java.util.List;

/**
 * 系统登录记录(SysLoginInfo)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysLoginInfoServiceImpl extends ServiceImpl<SysLoginInfoMapper, SysLoginInfo> implements SysLoginInfoService {

    /**
     * 构建查询条件
     *
     * @param sysLoginInfoBo 访问日志
     * @return 构建结果
     */
    private QueryWrapper buildQueryWrapper(SysLoginInfoBo sysLoginInfoBo) {
        Map<String, Object> params = sysLoginInfoBo.getParams();
        QueryWrapper query = query();
        query.between(SysLoginInfo::getLoginTime, params.get("beginTime"), params.get("endTime"),
                ObjectUtil.isNotNull(params.get("beginTime")) && ObjectUtil.isNotNull(params.get("endTime")));
        query.like(SysLoginInfo::getIpaddr, sysLoginInfoBo.getIpaddr(), StringUtils.isNotBlank(sysLoginInfoBo.getIpaddr()));
        query.eq(SysLoginInfo::getStatus, sysLoginInfoBo.getStatus(), StringUtils.isNotBlank(sysLoginInfoBo.getStatus()));
        query.like(SysLoginInfo::getUserName, sysLoginInfoBo.getUserName(), StringUtils.isNotBlank(sysLoginInfoBo.getUserName()));
        return query;
    }

    /**
     * 查询
     *
     * @param sysLoginInfoBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysLoginInfoVo> queryList(SysLoginInfoBo sysLoginInfoBo) {
        QueryWrapper query = buildQueryWrapper(sysLoginInfoBo);
        return listAs(query, SysLoginInfoVo.class);
    }

    /**
     * 通过infoId查询单条数据
     *
     * @param infoId 主键
     * @return 实例对象
     */
    @Override
    public SysLoginInfoVo queryById(Long infoId) {
        return getOneAs(query().eq(SysLoginInfo::getInfoId, infoId), SysLoginInfoVo.class);
    }

    /**
     * 分页查询
     *
     * @param sysLoginInfoBo 筛选条件
     * @param pageQuery      分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<SysLoginInfoVo> queryPageList(SysLoginInfoBo sysLoginInfoBo, PageQuery pageQuery) {
        QueryWrapper query = buildQueryWrapper(sysLoginInfoBo);
        pageQuery.buildOrder(query);
        Page<SysLoginInfoVo> page = pageAs(pageQuery.buildPage(), query, SysLoginInfoVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param sysLoginInfoBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(SysLoginInfoBo sysLoginInfoBo) {
        SysLoginInfo sysLoginInfo = MapstructUtils.convert(sysLoginInfoBo, SysLoginInfo.class);
        if (ObjectUtil.isNotNull(sysLoginInfo)) {
            sysLoginInfo.setLoginTime(new Date());
        }
        return save(sysLoginInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param infoIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> infoIds) {
        return removeByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLoginInfo() {
        remove(query());
    }
}
